Thumb

What is Jagged Array?

1/8/2020 2:02:29 AM

A special type of array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes also called as an” array of arrays”. You can declare a jagged array named scores of type String as string [][] scores;.  An array with n elements is indexed from 0 to n-1. Now given bellow the example of the Jagged Array code and explain the code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using testForClass1;

namespace testFor
{
    public class Program 
    {
        static void Main(string[] args)
        {
            string[] studentName = new string[3];
            studentName[0] = "Jesy";
            studentName[1] = "Reza";
            studentName[2] = "Tofail";
            //Creare Jagged Array
            string[][] jaggedArray = new string[3][];
            jaggedArray[0] = new string[3];
            jaggedArray[1] = new string[1];
            jaggedArray[2] = new string[2];
            //Student one
            jaggedArray[0][0] = "CSE";
            jaggedArray[0][1] = "BBA";
            jaggedArray[0][2] = "PHY";
            //Student Two
            jaggedArray[1][0] = "BBA";
            //Student Three
            jaggedArray[2][0] = "CSE";
            jaggedArray[2][1] = "PHY";
            for (int i = 0; i < jaggedArray.Length; i++)
            {
                Console.WriteLine();
                Console.WriteLine(studentName[i]);
                Console.WriteLine("....................................................");
                string[] innerArray = jaggedArray[i];
                for (int j = 0; j < innerArray.Length; j++)
                {
                    Console.WriteLine(innerArray[j]);
                }
            }
            Console.Read();
        }
    }
}

First, we create a simple array name as studentName then we create Jagged Array name as jaggedArray. This array is array of arrays. This array first contains the index then under the index contain the subject name. then we print the array of arrays by the nested for loop.